package qin.com;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;



@WebServlet(name = "ServletUsers",urlPatterns = {"/chengji"})

public class ServletChengJi extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String name=request.getParameter("name");
         String action = request.getParameter("action");
        double java = Double.parseDouble(request.getParameter("java"));
        double jsp = Double.parseDouble(request.getParameter("jsp"));
        double oracle = Double.parseDouble(request.getParameter("oracle"));


        ChengJi chengJi = null ;

        //添加用户信息

        if("insert".equals(action)){

            chengJi=new ChengJi();

            chengJi.setName(name);

            chengJi.setJava(java);
            chengJi.setJsp(jsp);
            chengJi.setOracle(oracle);
            if(ChengjiDao.insert(chengJi)){

                response.sendRedirect("listchengji.jsp");

            }else {

                response.getWriter().println("添加用户信息失败<a href=listusers.jsp>跳转到用户列表</a>");

            }

        }

        //更新用户信息

        if("update".equals(action)){

            int id = Integer.parseInt(request.getParameter("id"));

            chengJi=new ChengJi();
            chengJi.setName(name);

            chengJi.setJava(java);
            chengJi.setJsp(jsp);
            chengJi.setOracle(oracle);

            if(ChengjiDao.update(chengJi)){

                response.sendRedirect("listchengji.jsp");

            }else {

                response.getWriter().println("更新用户信息失败<a href=listusers.jsp>跳转到用户列表</a>");

            }

        }

        //删除用户信息

        if("delete".equals(action)){

            int id = Integer.parseInt(request.getParameter("id"));

            if(ChengjiDao.delete(id)){

                response.sendRedirect("listchengji.jsp");

            }else {

                response.getWriter().println("删除用户信息失败<a href=listusers.jsp>跳转到用户列表</a>");

            }

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        response.setContentType("text/html; charset=utf-8");
        doPost(request, response);

    }

}